package com.airticketbooking.controller.manager;

import com.airticketbooking.common.constant.JwtClaimsConstant;
import com.airticketbooking.common.domain.PageDTO;
import com.airticketbooking.common.domain.PageQuery;
import com.airticketbooking.common.domain.R;
import com.airticketbooking.common.properties.JwtProperties;
import com.airticketbooking.common.utils.JwtUtil;
import com.airticketbooking.common.utils.Md5Util;
import com.airticketbooking.po.Employee;
import com.airticketbooking.vo.EmployeeLoginVO;
import com.airticketbooking.service.EmployeeService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;

/**
 * @author Mr.J
 * @version 1.0.0
 * @date 2024/11/10
 * @desc
 */

@RestController
@RequiredArgsConstructor
@Api(tags = "员工管理")
@RequestMapping("/air/manager/employee")
public class EmployeeController {


	private final JwtProperties jwtProperties;

	private final EmployeeService employeeService;

	//	员工登录接口
	@GetMapping("/login")
	public R<EmployeeLoginVO> emplogin(String employeeName, String password) {

		Employee employee = employeeService.getByEmployeeName(employeeName);

		if (employee == null) {

			return R.error("用户名或密码错误");
		}
		if (!employee.getPassword().equals(Md5Util.getMD5String(password))) {

			return R.error("用户名或密码错误");
		}
		if (employee.getStatus() == 0) {

			return R.error("账号已被禁用");
		}


//		生成token

		HashMap<String, Object> claims = new HashMap<>();

		claims.put(JwtClaimsConstant.EMP_ID, employee.getEmployeeId());

		String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),
				jwtProperties.getAdminTtl(),
				claims);

		EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO();
		employeeLoginVO.setEmployeeId(employee.getEmployeeId());
		employeeLoginVO.setEmployeeName(employee.getEmployeeName());
		employeeLoginVO.setToken(token);

		return R.ok(employeeLoginVO);
	}

	//	员工注册接口
	@GetMapping("/register")
	public R<String> register(String employeeName, String password, String rePassword) {


		try {
			employeeService.register(employeeName, password, rePassword);
			return R.ok("注册成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}

	}

	//	员工信息修改接口
	@PutMapping("/modify")
	public R<String> modify(@RequestBody Employee employee) {


		try {
			employeeService.modify(employee);
			return R.ok("修改成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	//	通过员工id获取员工信息接口
	@GetMapping("/getEmployeeById/{employeeId}")
	public R<Employee> getById(@PathVariable Integer employeeId) {

		if (employeeId == null) {
			return R.error("Employee id cannot be null");
		}

		Employee employee = employeeService.getById(employeeId);
		if (employee == null) {
			return R.error("Employee not found");
		}

		return R.ok(employee);
	}


	@PostMapping("/getEmployeeInfo")
	public R<Employee> getEmployeeInfo() {


		try {

			Employee employee = employeeService.getEmployeeInfo();
			return R.ok(employee);
		} catch (Exception e) {

			return R.error(e.getMessage());
		}

	}


	//分页查询员工信息接口
	@GetMapping("/getEmployees")
	public R<PageDTO<Employee>> getempPage(PageQuery pageQuery) {

		Page<Employee> empPages = employeeService.getempPage(pageQuery);
		PageDTO<Employee> pageDTO = new PageDTO<>();
		pageDTO.setTotal(empPages.getTotal());
		pageDTO.setList(empPages.getResult());
		return R.ok(pageDTO);
	}

	//	禁用/启用用户接口
	@PutMapping("/openOrBanUser/{userId}")
	@ApiOperation(value = "禁用/启用员工接口")
	public R<String> openOrBanUser(@PathVariable Integer userId) {

		Employee empById = employeeService.getById(userId);


		Employee employee = new Employee();

		if (empById.getStatus() == 1) {
			employee.setEmployeeId(userId);
			employee.setStatus(0);
			employeeService.modify(employee);
			return R.ok("员工禁用成功");

		} else {
			employee.setEmployeeId(userId);
			employee.setStatus(1);
			employeeService.modify(employee);
			return R.ok("员工启用成功");
		}

	}


//	更改密码接口
	@GetMapping("/changePassword")
	@ApiOperation(value = "更改密码接口")
	public R<String> changePassword(String oldPassword, String newPassword, String rePassword) {

		try {
			employeeService.changePassword(oldPassword, newPassword, rePassword);
			return R.ok("密码修改成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}


//	更改头像接口
	@GetMapping("/updateAvatar")
	@ApiOperation(value = "更改头像接口")
	public R<String> updateAvatar(String avatarUrl) {

		try {

			employeeService.changeAvatar(avatarUrl);
			return R.ok("头像修改成功");
		} catch (Exception e) {


			return R.error(e.getMessage());
		}
	}

}
